import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../common_ui/back_nav.dart';
import '../../common_ui/circle_avatar.dart';
import '../../common_ui/confirm_btn.dart';
import '../../const/common_const.dart';
import '../../style/color_style.dart';
import '../../utils/sp_utils.dart';
import '../controller/photo_album_controller.dart';

class PhotoAlbumPage extends StatelessWidget {
  PhotoAlbumPage({Key? key}) : super(key: key);
  final PhotoAlbumController controller = Get.put(PhotoAlbumController());
  final itemWH = (MediaQuery.of(Get.context!).size.width - 2 * 50 - 30) / 2;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorStyle.whiteColor,
        appBar: BackNavigation(title: MineStringConst.photoAlbumText),
        body: GetBuilder<PhotoAlbumController>(builder: ((controller) {
          return controller.userData2 == null
              ? const Center(
                  child: Text('加载中...'),
                )
              : CustomScrollView(
                  slivers: [_headerWidget(), _photoWidget(), _bottomWidget()]);
        })));
  }

  Widget _bottomWidget() {
    return SliverToBoxAdapter(
        child: Container(
      padding: EdgeInsetsStyle.customMargin(20, 16, 20, 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '注：不得上传违规图片，待审核通过后在个人主页展示',
            style: Styles.greyText_12,
          ),
          Box.vBox30,
          _bottomItemWidget(
              '昵称', controller.userData2?.userinfo?.nickname ?? ''),
          _bottomItemWidget(
              '生日', controller.userData2?.userinfo?.birthday ?? ''),
          Container(
            padding: EdgeInsetsStyle.edgeInsets(90, 20),
            child: ConfirmButton(
              borderRaduis: 8,
              backGroundColor: ColorStyle.themeColor,
              titleColor: ColorStyle.whiteColor,
              title: '确定',
              callBack: () {
                controller.submitClick();
              },
            ),
          )
        ],
      ),
    ));
  }

  Widget _bottomItemWidget(String title, String des) {
    return GestureDetector(
      child: Container(
          color: ColorStyle.whiteColor,
          padding: EdgeInsetsStyle.customMargin(0, 10, 0, 20),
          child: Row(children: [
            Text(
              title,
              style: Styles.greyText_14,
            ),
            const Spacer(),
            Text(
              des,
              style: Styles.greyText_14,
            ),
            Box.hBox15,
            const Icon(
              Icons.arrow_forward_ios_rounded,
              size: 13,
            )
          ])),
      onTap: () {
        controller.bottomItemClick(title);
      },
    );
  }

  Widget _headerWidget() {
    return SliverToBoxAdapter(
      child: Container(
        padding: EdgeInsetsStyle.customMargin(20, 16, 20, 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '修改头像',
              style: Styles.normalText_16,
            ),
            Box.vBox12,
            GestureDetector(
              child: CircleAvatarWidget(
                imgHeight: 60,
                imgWidth: 60,
                url: controller.userData2!.userinfo?.avatar ?? '',
                gender: controller.userData2!.userinfo?.gender ?? 1,
              ),
              onTap: () {
                controller.updateAvatar();
              },
            ),
            Box.vBox5,
            Text(
              '注：请上传本人高清头像，等待审核通过后才可展示',
              style: Styles.greyText_12,
            ),
            Box.vBox12,
            Text(
              '我的相册',
              style: Styles.normalText_16,
            ),
          ],
        ),
      ),
    );
  }

  Widget _photoWidget() {
    return SliverToBoxAdapter(
        child: Container(
      color: Colors.white,
      alignment: Alignment.center,
      child: Wrap(
        spacing: 20,
        runSpacing: 20,
        children: [
          _photoItemWidget(0),
          _photoItemWidget(1),
          _photoItemWidget(2),
          _photoItemWidget(3),
        ],
      ),
    ));
  }

  Widget _photoItemWidget(int index) {
    if (controller.userData2!.userinfo!.albumList == null) {
      return Container(
        width: itemWH,
        height: itemWH,
        decoration: BoxDecoration(
            borderRadius: RadiusStyle.radiusAll(6),
            color: ColorStyle.f5f5f5Color),
        child: Center(
            child: Stack(
          children: [Image.asset(ImagePath.assetsPhotoAdd)],
        )),
      );
    }
    String url = '';
    if (index == 0) {
      if (controller.userData2!.userinfo!.albumList!.isNotEmpty) {
        url = controller.userData2!.userinfo!.albumList![0];
      }
    } else if (index == 1) {
      if (controller.userData2!.userinfo!.albumList!.length > 1) {
        url = controller.userData2!.userinfo!.albumList![1];
      }
    } else if (index == 2) {
      if (controller.userData2!.userinfo!.albumList!.length > 2) {
        url = controller.userData2!.userinfo!.albumList![2];
      }
    } else if (index == 3) {
      if (controller.userData2!.userinfo!.albumList!.length > 3) {
        url = controller.userData2!.userinfo!.albumList![3];
      }
    }
    return Container(
      width: itemWH,
      height: itemWH,
      decoration: BoxDecoration(
          borderRadius: RadiusStyle.radiusAll(6),
          color: ColorStyle.f5f5f5Color),
      child: Center(
          child: Stack(
        children: [
          url != ''
              ? Stack(children: [
                  CachedNetworkImage(
                    fit: BoxFit.fitWidth,
                    imageUrl: SpUtil.getImageUrl(url),
                    placeholder: (context, str) {
                      return Image.asset(
                          controller.userData2?.userinfo?.gender == 1
                              ? ImagePath.assetsImagesHeaderForWomen
                              : ImagePath.assetsImagesHeaderForMan);
                    },
                    errorWidget: (context, str, obj) {
                      return Image.asset(
                          controller.userData2?.userinfo?.gender == 1
                              ? ImagePath.assetsImagesHeaderForWomen
                              : ImagePath.assetsImagesHeaderForMan);
                    },
                  ),
                  GestureDetector(
                    child: Image.asset(ImagePath.assetsDeleteBtn),
                    onTap: () {
                      controller.deletePhoto(index);
                    },
                  )
                ])
              : GestureDetector(
                  child: Image.asset(ImagePath.assetsPhotoAdd),
                  onTap: () {
                    controller.addPhoto();
                  },
                )
        ],
      )),
    );
  }
}
